﻿#include "TreeWidgetLoad.h"
#include "TreeItemTypes.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSolverLoadCase.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractLoad.h"

#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"

#include <QMenu>

#define _DATAOBJIDCOLUMN_ 0
#define _DATAOBJID_ 1

namespace GUI
{
    TreeWidgetLoad::TreeWidgetLoad(QWidget* parent /*= nullptr*/)
        :TreeWidgetPage(parent)
    {
        this->blockSignals(true);
        this->initTree();
        this->expandAll();
        this->blockSignals(false);
    }

    TreeWidgetType TreeWidgetLoad::getTreeWidgetType()
    {
        return PageLoad;
    }

    void TreeWidgetLoad::updateLoadCaseTreeItemState()
    {
        QSignalBlocker blocker(this);
        //获取载荷工况管理器
        Acoustics::FITKAcousticsPhysics* physicsData = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (!physicsData) return;
        Acoustics::FITKSolverLoadCaseManager* loadCaseManager = physicsData->getSolverLoadCaseManager();
        if (!loadCaseManager) return;
        // 获取载荷工况根节点
        int rootCount = this->topLevelItemCount();
        if (rootCount == 0)return;
        QTreeWidgetItem* loadCaseTreeRoot = this->topLevelItem(0);
        if (!loadCaseTreeRoot) return;
        // 更新载荷工况根节点的状态
        bool stateLoadCaseRoot = loadCaseManager->isEnable();
        loadCaseTreeRoot->setCheckState(0, stateLoadCaseRoot ? Qt::Checked : Qt::Unchecked);
        // 获取载荷工况的树节点数量
        int loadCaseTreeCount = loadCaseTreeRoot->childCount();
        for (int i = 0; i < loadCaseTreeCount; ++i)
        {
            //获取载荷工况的树节点
            QTreeWidgetItem* loadCaseTreeNode = loadCaseTreeRoot->child(i);
            if (!loadCaseTreeNode) continue;
            //获取载荷工况ID
            int loadCaseID = loadCaseTreeNode->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();
            // 获取载荷工况数据
            Acoustics::FITKAcousticsSolverLoadCase* loadCaseObj = loadCaseManager->getDataByID(loadCaseID);
            if (!loadCaseObj) continue;
            // 更新载荷工况节点的状态
            bool stateLoadCase = loadCaseObj->isEnable();
            loadCaseTreeNode->setCheckState(0, stateLoadCase ? Qt::Checked : Qt::Unchecked);
            // 获取载荷树节点数据数量
            int loadTreeCount = loadCaseTreeNode->childCount();
            for (int j = 0; j < loadTreeCount; ++j)
            {
                // 查找对应的子节点
                QTreeWidgetItem* dataTreeNode = loadCaseTreeNode->child(j);
                if (!dataTreeNode) continue;
                //获取载荷ID
                int loadID = dataTreeNode->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();
                // 获取数据
                Core::FITKAbstractNDataObject* data = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(loadID);
                if (!data) continue;
                // 更新子节点的状态
                bool state = data->isEnable();
                dataTreeNode->setCheckState(0, state ? Qt::Checked : Qt::Unchecked);
            }
        }
    }

    void TreeWidgetLoad::updateNodeName(int objID)
    {
        //获取数据名称
        Core::FITKAbstractNDataObject* data = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(objID);
        if (!data) return;
        QString name = data->getDataObjectName();
        //对应数据的树节点
        QTreeWidgetItem* item = nullptr;
        for (int i = 0; i < this->topLevelItemCount(); ++i)
        {
            QTreeWidgetItem * root = this->topLevelItem(i);
            if (!root)continue;
            item = this->getItemByObjID(objID, root);
            if (item) break;
        }
        if (!item) return;
        item->setText(0, name);
    }

    void TreeWidgetLoad::updateLoadCaseTree()
    {
        this->blockSignals(true);
        this->clear();
        //获取载荷工况管理器
        Acoustics::FITKAcousticsPhysics* physicsData = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (!physicsData) return;
        Acoustics::FITKSolverLoadCaseManager* loadCaseManager = physicsData->getSolverLoadCaseManager();
        if (!loadCaseManager) return;
        bool stateLoadCaseRoot = loadCaseManager->isEnable();
        //LoadCase根节点
        QTreeWidgetItem* loadCaseTreeRoot = this->CreateTreeRootItem(this, tr("Load Cases"), LoadCaseTreeItemsType::LTLoadCaseRoot, "", stateLoadCaseRoot);
        loadCaseTreeRoot->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, loadCaseManager->getDataObjectID());
        //获取数量
        int count = loadCaseManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            //获取组数据
            Acoustics::FITKAcousticsSolverLoadCase* loadCaseObj = loadCaseManager->getDataByIndex(i);
            if (!loadCaseObj) continue;
            //获取组ID, 名称和状态
            int loadCaseDataID = loadCaseObj->getDataObjectID();
            QString loadCaseName = loadCaseObj->getDataObjectName();
            bool stateLoadCase = loadCaseObj->isEnable();
            //创建载荷工况节点
            QTreeWidgetItem* loadCaseTreeNode = this->CreateTreeItem(loadCaseTreeRoot, loadCaseName, LoadCaseTreeItemsType::LTLoadCase, "", stateLoadCase);
            loadCaseTreeNode->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, loadCaseDataID);
            //获取载荷工况管理的数据
            int loadCount = loadCaseObj->getLoadIDs().size();
            for(int j = 0; j < loadCount; ++j)
            {
                //获取数据
                Core::FITKAbstractNDataObject* data = loadCaseObj->getLoadDataObjAt(j);
                if (!data) continue;
                //获取数据
                bool state = data->isEnable();
                QString name = data->getDataObjectName();
                //创建载荷工况节点
                QTreeWidgetItem* dataTreeNode = this->CreateTreeItem(loadCaseTreeNode, name, LoadCaseTreeItemsType::LTLoadCaseChild, "", state);
                dataTreeNode->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, data->getDataObjectID());
            }
        }
        this->blockSignals(false);
    }

    void TreeWidgetLoad::initTree()
    {
        //获取载荷工况管理器
        Acoustics::FITKAcousticsPhysics* physicsData = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (!physicsData) return;
        Acoustics::FITKSolverLoadCaseManager* loadCaseManager = physicsData->getSolverLoadCaseManager();
        if (!loadCaseManager) return;
        bool stateLoadCaseRoot = loadCaseManager->isEnable();
        //载荷工况根节点
        QTreeWidgetItem* loadCaseRoot = this->CreateTreeRootItem(this, tr("Load Cases"), LoadCaseTreeItemsType::LTLoadCaseRoot, "", stateLoadCaseRoot);
        loadCaseRoot->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, loadCaseManager->getDataObjectID());
    }

    void TreeWidgetLoad::on_itemDoubleClicked(QTreeWidgetItem *item, int column)
    {

    }

    void TreeWidgetLoad::on_itemStateChanged(QTreeWidgetItem *item, bool state, int column)
    {
        QString operName = "";
        // 根据 state 决定操作器
        if (state)
            operName = "actionLoadCaseShow";
        else
            operName = "actionLoadCaseHide";

        // 创建一个QObject对象
        QObject* sender = new QObject;
        sender->setObjectName(operName);

        // 从仓库中获取指定名称的操作器
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(operName);
        if (oper == nullptr) return;

        QList<QTreeWidgetItem*> items;
        items.append(item);
        // 调用操作器执行操作
        oper->setEmitter(sender);
        oper->setArgs("SelectedItems", QVariant::fromValue((void*)(&items)));
        oper->actionTriggered();

        // 释放QObject对象
        delete sender;
    }

    void TreeWidgetLoad::on_itemContextMenu(const QList<QTreeWidgetItem*>& item, QMenu* menu)
    {
        if (item.isEmpty() || !menu) return;
        //获取数量和类型
        int num = 0;
        LoadCaseTreeItemsType type = LoadCaseTreeItemsType::LTNone;
        for (QList<QTreeWidgetItem*>::const_iterator iter = item.begin(); iter != item.end(); ++iter)
        {
            if (!(*iter))continue;
            type = LoadCaseTreeItemsType((*iter)->type());
            ++num;
        }
        //创建动作
        if (num == 1)
        {
            if (type == LoadCaseTreeItemsType::LTLoadCaseRoot)
                this->menuAddAction(menu, "actionCreateLoadCase", tr("Create Load Case"));
            else
                this->menuAddAction(menu, "actionLoadCaseEdit", tr("Edit"));
        }
        this->menuAddAction(menu, "actionLoadCaseShow", tr("Show"));
        this->menuAddAction(menu, "actionLoadCaseHide", tr("Hide"));
        this->menuAddAction(menu, "actionLoadCaseShowOnly", tr("ShowOnly"));
    }

    void TreeWidgetLoad::menuAddAction(QMenu* menu, QString objName, QString textName, QString iconFileName)
    {
        if (!menu) return;
        //菜单栏添加工具
        QAction* action = menu->addAction(QIcon(iconFileName), textName);
        action->setObjectName(objName);
    }

    QTreeWidgetItem* TreeWidgetLoad::getItemByObjID(int objID, QTreeWidgetItem * item)
    {
        if (!item) return nullptr;
        if (item->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt() == objID)
            return item;
        for (int i = 0; i < item->childCount(); ++i)
        {
            QTreeWidgetItem * itemChild = item->child(i);
            if (!itemChild)continue;
            QTreeWidgetItem * nodeItem = getItemByObjID(objID, itemChild);
            if (nodeItem) return nodeItem;
        }
        return nullptr;
    }
}


